package com.weather.controller;

import com.weather.domain.City;
import com.weather.domain.WeatherResponse;
import com.weather.domain.WeatherResult;
import com.weather.service.CityService;
import com.weather.service.WeatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

//http://localhost:8080/weather
@Controller//返回文本或跳转页面
public class HechiController {
    @Autowired
    CityService cityService;
    @Autowired
    WeatherService weatherService;
    @RequestMapping("/weather")
    public ModelAndView weather(Model model, @RequestParam(defaultValue = "110100") String city){
        List<City> cityList=cityService.getAllCities();
        WeatherResponse weatherResponse=weatherService.queryWeatherForUser(city);

        System.out.println("weatherResponse:"+weatherResponse);
        System.out.println("result:"+weatherResponse.getResult());
        System.out.println("List:"+weatherResponse.getResult().getList().size());


        model.addAttribute("list",cityList);
        model.addAttribute("weatherData",weatherResponse);
        model.addAttribute("city", city);


        return new ModelAndView("result");
    }
}
